package io.virtualapp.hook_sdk.firewall;

import android.text.TextUtils;
import android.util.Log;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import io.virtualapp.hook_sdk.dao.RiNotesService;
import io.virtualapp.hook_sdk.models.NotesBean;

public class FireWallUtils {

    /**
     * 白名单校验规则
     * @param sourceUrl 原始路径
     * @return 白名单校验过的新路径
     * */
    private static String whileCheck(String sourceUrl){
        if (TextUtils.isEmpty(sourceUrl))
            return FireWallConfig.hookUrl;
        if (FireWallConfig.array == null)
            return FireWallConfig.hookUrl;
        if (FireWallConfig.array.size() == 0)
            return FireWallConfig.hookUrl;
        ArrayList<AppNetInfo> array = getHost();
        for (AppNetInfo info : array){
            if (info.addressStr.equals(sourceUrl)) {
                NotesBean bean = new NotesBean();
                bean.app_adopt = true;
                bean.app_id = RiNotesService.getInstance().selApp(RiNotesService.getInstance().app_name);
                bean.app_interface = sourceUrl;
                bean.interface_type = 2;
                RiNotesService.getInstance().insertNotes(bean);
                return sourceUrl;
            }
        }
        NotesBean bean = new NotesBean();
        bean.app_adopt = false;
        bean.app_id = RiNotesService.getInstance().selApp(RiNotesService.getInstance().app_name);
        bean.app_interface = sourceUrl;
        bean.interface_type = 2;
        boolean flag = RiNotesService.getInstance().insertNotes(bean);
        Log.e("ZXY","插入日志:"+flag);
        return FireWallConfig.hookUrl;
    }
    /**
     * 黑名单校验规则
     * */
    private static String blackCheck(String sourceUrl){
        if (TextUtils.isEmpty(sourceUrl))
            return FireWallConfig.hookUrl;
        if (FireWallConfig.array == null)
            return FireWallConfig.hookUrl;
        if (FireWallConfig.array.size() == 0)
            return FireWallConfig.hookUrl;
        boolean hookFlag = true;
        ArrayList<AppNetInfo> array = getHost();
        for (AppNetInfo info : array){
            if (info.addressStr.equals(sourceUrl)) {
                hookFlag = false;
                break;
            }
        }
        if (!hookFlag) {
            NotesBean bean = new NotesBean();
            bean.app_adopt = false;
            bean.app_id = RiNotesService.getInstance().selApp(RiNotesService.getInstance().app_name);
            bean.app_interface = sourceUrl;
            bean.interface_type = 3;
            RiNotesService.getInstance().insertNotes(bean);
            return FireWallConfig.hookUrl;
        }
        NotesBean bean = new NotesBean();
        bean.app_adopt = true;
        bean.app_id = RiNotesService.getInstance().selApp(RiNotesService.getInstance().app_name);
        bean.app_interface = sourceUrl;
        bean.interface_type = 3;
        RiNotesService.getInstance().insertNotes(bean);
        return sourceUrl;
    }

    /**
     *防火墙校验
     * @param type 防火墙类型
     * @param sourceUrl 原始路径
     * @return 检验后路径
     */
    public static String firewallOkHttpCheck(FireWallType type,String sourceUrl){
        if (FireWallType.NONE == type) {
            //添加无拦截日志
            NotesBean bean = new NotesBean();
            bean.app_adopt = true;
            bean.app_id = RiNotesService.getInstance().selApp(RiNotesService.getInstance().app_name);
            bean.app_interface = sourceUrl;
            bean.interface_type = 1;
            RiNotesService.getInstance().insertNotes(bean);
            return sourceUrl;
        }
        String url = sourceUrl;
        if (FireWallType.WHITE == type) {
            String resultStr = whileCheck(url);
            if (resultStr.equals(url))
                return sourceUrl;
            else
                return FireWallConfig.hookUrl;
        }
        if (FireWallType.BLACK == type){
            String resultStr = blackCheck(url);
            if (resultStr.equals(url))
                return sourceUrl;
            else
                return FireWallConfig.hookUrl;
        }
        return FireWallConfig.hookUrl;
    }

    /**
     *防火墙校验
     * @param type 防火墙类型
     * @param sourceUrl 原始路径
     * @return 检验后路径
     */
    public static String firewallCheck(FireWallType type,String sourceUrl){
        if (FireWallType.NONE == type) {
            //添加无拦截日志
            NotesBean bean = new NotesBean();
            bean.app_adopt = true;
            bean.app_id = RiNotesService.getInstance().selApp(RiNotesService.getInstance().app_name);
            bean.app_interface = sourceUrl;
            bean.interface_type = 1;
            RiNotesService.getInstance().insertNotes(bean);
            return sourceUrl;
        }
        String url = null;
        try {
            url = new URL(null,sourceUrl).getHost();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        if (FireWallType.WHITE == type) {
            String resultStr = whileCheck(url);
            if (resultStr.equals(url))
                return sourceUrl;
            else
                return FireWallConfig.hookUrl;
        }
        if (FireWallType.BLACK == type){
            String resultStr = blackCheck(url);
            if (resultStr.equals(url))
                return sourceUrl;
            else
                return FireWallConfig.hookUrl;
        }
        return FireWallConfig.hookUrl;
    }

    private static ArrayList<AppNetInfo> getHost(){
        ArrayList<AppNetInfo> array = new ArrayList<>();
        //数据脱壳操作
        for (int i = 0;i < FireWallConfig.array.size();i ++){
            AppNetInfo info = FireWallConfig.array.get(i);
            //获取域名
            //去掉http://
            String hostName = null;
            try {
                hostName = new URL(null,info.addressStr).getHost();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            AppNetInfo nowInfo = new AppNetInfo(null,hostName,null);
            array.add(nowInfo);
        }
        return array;
    }
}
